package com.zsj.comminlibrary.util.log;

import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by Administrator on 2018/8/22.
 */

public class DiskLogHandler extends Handler{
    private final String folder;
    private final int maxFileSize;
    private final String fileName;

    public DiskLogHandler(String folder, String fileName, int maxFileSize) {
        this(getDefaultLooper(), folder, fileName, maxFileSize);
    }

    public DiskLogHandler(Looper looper, String folder, String fileName, int maxFileSize) {
        super(looper);
        this.folder = folder;
        this.fileName = fileName;
        this.maxFileSize = maxFileSize;
    }

    private static Looper getDefaultLooper() {
        HandlerThread ht = new HandlerThread("AndroidFileLogger");
        ht.start();
        return ht.getLooper();
    }

    @SuppressWarnings("checkstyle:emptyblock")
    @Override
    public void handleMessage(Message msg) {
        try {
            String content = (String) msg.obj;

            FileWriter fileWriter = null;
            File logFile = getLogFile(folder, fileName);

            try {
                fileWriter = new FileWriter(logFile, true);

                writeLog(fileWriter, content);

                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                if (fileWriter != null) {
                    try {
                        fileWriter.flush();
                        fileWriter.close();
                    } catch (IOException e1) { /* fail silently */ }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * This is always called on a single background thread.
     * Implementing classes must ONLY write to the fileWriter and nothing more.
     * The abstract class takes care of everything else including close the stream and catching IOException
     *
     * @param fileWriter an instance of FileWriter already initialised to the correct file
     */
    private void writeLog(FileWriter fileWriter, String content) throws IOException {
        fileWriter.append(content);
    }

    private File getLogFile(String folderName, String fileName) {
        File folder = new File(folderName);
        if (!folder.exists()) {
            //TODO: What if folder is not created, what happens then?
            folder.mkdirs();
        }

        int newFileCount = 0;
        File newFile;
        File existingFile = null;

        newFile = new File(folder, String.format("%s_%s.csv", fileName, newFileCount));
        while (newFile.exists()) {
            existingFile = newFile;
            newFileCount++;
            newFile = new File(folder, String.format("%s_%s.csv", fileName, newFileCount));
        }

        if (existingFile != null) {
            if (existingFile.length() >= maxFileSize) {
                writeFile(newFile);
                return newFile;
            } else {
                newFile= existingFile;
            }
        }

        return newFile;

    }

    private void writeFile(File newFile) {
        FileWriter fileWriter = null;

        try {

            fileWriter = new FileWriter(newFile, true);
            writeLog(fileWriter, "手机型号:"+ Build.MODEL+"\n");
            String appVersion = "";
            try {
                Class<?> aClass = Class.forName("com.sds.commonlibrary.util.UIUtils");
                Method method = aClass.getMethod("getVersion");
                appVersion = (String) method.invoke(null);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            writeLog(fileWriter, "app版本号:"+appVersion+"\n\n");
            fileWriter.flush();
            fileWriter.close();
        } catch (Exception e) {
            if (fileWriter != null) {
                try {
                    fileWriter.flush();
                    fileWriter.close();
                } catch (IOException e1) { /* fail silently */ }
            }
        }

    }
}
